//
//  HWsXiaoHaoGuanLiViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/11/14.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWsXiaoHaoGuanLiViewController.h"
#import "HWXiaoHaoableViewCell.h"
#import "HWXiaoHaoCustomHeadFooterView.h"
#import "HWXiaoGroupModel.h"
#import "HWViewSellNoController.h"


@interface HWsXiaoHaoGuanLiViewController ()<UITableViewDataSource,UITableViewDelegate,HWXiaoHaoCustomHeadFooterViewDelegate>
@property(strong,nonatomic)UITableView *mainaTableVie;
@property(strong,nonatomic)NSMutableArray *dataMuArr;
@property(assign,nonatomic) int xuanzhong;
@end

@implementation HWsXiaoHaoGuanLiViewController

-(UITableView *)mainaTableVie {

    if (_mainaTableVie == nil
        ) {

         _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,kScreenH) style:UITableViewStylePlain];
         _mainaTableVie.delegate = self;
         _mainaTableVie.dataSource = self;
         _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
         _mainaTableVie.backgroundColor = [UIColor whiteColor];
         _mainaTableVie.sectionHeaderHeight = 44;
        

    }
    return   _mainaTableVie ;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
    [self fasongwangluoqingqiu];
    
}

// 发送网路请求
- (void)fasongwangluoqingqiu {
    
    
      self.dataMuArr = [[NSMutableArray alloc]init];
    
       NSString *userID = [HWLocallyStoredData getLoginUserID];
    
       int  suiJi =  [WMUtil getRandomNumber:101 to:998];
       NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],suiJi];
       NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
       NSString *signMD5 = [WMUtil md5HexDigest:sign];
       NSDictionary *parametes = @{
                                   @"ct" :  @"app",
                                   @"ac" : @"small_search",
                                   @"ios" : @"1",
                                   @"uid" :userID,
                                   @"t" :tStr,
                                   @"sign": signMD5
                                   };
       
       

       
       [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
           
         
           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        //   [MBProgressHUD hideHUD];
           
           
           if ([codeStr intValue] == 1) {
            
             self.dataMuArr  = [HWXiaoGroupModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
              
               
               

           } else {
               
                [MBProgressHUD  showError:response[@"message"]];
               
           }
           
           [self.mainaTableVie reloadData];
           NSLog(@"%@",response);
           
           
       } failure:^(NSString *error) {
         
           [MBProgressHUD  showError:@"加载失败"];
           
       }];
       
    
    
    
}

// 设置UI
- (void)setupUI {
    
    self.navigationItem.title = @"小号管理";
    self.navigationItem.title  = [HWjianzhiduiObject keylocalization:@"小号管理"];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view  addSubview:self.mainaTableVie];
    [self addheadeView];
}


// 小号注册
- (void)addheadeView{
    
    
    UIView *headeview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 40)];
    self.mainaTableVie.tableHeaderView = headeview;
    
      UILabel  *xiahao1mingz  = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenW/3, 40)];
      xiahao1mingz.text = @"小号名字";
      xiahao1mingz.text  =  [HWjianzhiduiObject keylocalization:@"小号名字"];
      xiahao1mingz.font = [UIFont systemFontOfSize:13];
      xiahao1mingz.textColor = RGB(51,51,51);
      xiahao1mingz.numberOfLines = 2;
      xiahao1mingz.backgroundColor = RGB(233,241, 255);
      xiahao1mingz.textAlignment = NSTextAlignmentCenter;
      [headeview addSubview:xiahao1mingz];
                  
       UILabel  *zhuceshijian  = [[UILabel alloc]initWithFrame:CGRectMake(kScreenW/3, 0, kScreenW/3, 40)];
         zhuceshijian.text = @"注册时间";
    zhuceshijian.text  =  [HWjianzhiduiObject keylocalization:@"注册时间"];
         zhuceshijian.font = [UIFont systemFontOfSize:13];
         zhuceshijian.textColor = RGB(51,51,51);
          zhuceshijian.numberOfLines = 2;
          zhuceshijian.textAlignment = NSTextAlignmentCenter;
        zhuceshijian.backgroundColor = RGB(233,241, 255);
        [headeview addSubview:zhuceshijian];
    
        UILabel  *caozuo  = [[UILabel alloc]initWithFrame:CGRectMake(kScreenW/3*2, 0, kScreenW/3, 40)];
           caozuo.text = @"操作";
         caozuo.text  =  [HWjianzhiduiObject keylocalization:@"注册时间"];
           caozuo.font = [UIFont systemFontOfSize:13];
           caozuo.textColor = RGB(51,51,51);
            caozuo.numberOfLines = 2;
          caozuo.backgroundColor = RGB(233,241, 255);
            caozuo.textAlignment = NSTextAlignmentCenter;
          [headeview addSubview:caozuo];
    
    
      UIView *shuxian2 = [[UIView alloc]initWithFrame:CGRectMake(kScreenW/3*1, 0, 1, 40)];
      shuxian2.backgroundColor = RGB(232, 232, 242);
      [headeview addSubview:shuxian2];
                   
                   
      UIView *shuxian3 = [[UIView alloc]initWithFrame:CGRectMake(kScreenW/3*2, 0, 1, 40)];
      shuxian3.backgroundColor = RGB(232, 232, 242);
      [headeview addSubview:shuxian3];
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
  
    
    
    
    return  self.dataMuArr.count;
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
   HWXiaoGroupModel *group = self.dataMuArr[section];
    
    if (group.visible) {
           return group.data.count;
           
           
    }else{
           return 0;
    }
       
    
  
  
    // return  self.dataMuArr;
}

 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    
    static NSString *cellID = @"cellID";
    
    HWXiaoHaoableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[HWXiaoHaoableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
     HWXiaoGroupModel *model = self.dataMuArr[indexPath.section];
    
     HWYouXixiaoHaoModel *xiaohao = model.data[indexPath.row];
    cell.model = xiaohao;
    
    
    return cell;
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return  40;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
   HWXiaoGroupModel  *group = self.dataMuArr[section];
   HWXiaoHaoCustomHeadFooterView *view = [HWXiaoHaoCustomHeadFooterView headFooterViewWithTableview:tableView];
    view.delegate = self;
    view.group = group;
    view.tag = section;
   // view.backgroundColor = [UIColor redColor];
    //return 返回之前headerFooterView的frame是0,所以需要在某个地方设置headerFooterView的frame
    return view;
    //return 返回之后，uitableview在用headerFooterView的时候就会设置headerFooterView的frame
}


//
- (void)groupHeaderViewDidClickTitleButton:(HWXiaoHaoCustomHeadFooterView *)headerview {
    
    self.xuanzhong = (int)headerview.tag;
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:headerview.tag];
    [self.mainaTableVie reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
    
    
    
    
    
}





- (void)delagateHeaderViewDidClickTitleButton:(HWXiaoHaoCustomHeadFooterView *)headerview {
    
     NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:headerview.tag];

     self.xuanzhong = (int)headerview.tag;
    
      UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"确定要删除小号" message:@"删除小号会，连旗下的记录一并删除" preferredStyle:UIAlertControllerStyleAlert];
      //默认只有标题 没有操作的按钮:添加操作的按钮 UIAlertAction
      
      UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
          NSLog(@"取消");
          
          
          
      }];
      //添加确定
      UIAlertAction *sureBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull   action) {
          NSLog(@"确定");
          [self shachuxiaohao];
          
          
      }];
      //设置`确定`按钮的颜色
      [sureBtn setValue:[UIColor redColor] forKey:@"titleTextColor"];
      //将action添加到控制器
      [alertVc addAction:cancelBtn];
      [alertVc addAction :sureBtn];
      //展示
      [self presentViewController:alertVc animated:YES completion:nil];
    
    
    
}

//
// 出售
- (void)chushouViewtititleButton:(HWXiaoHaoCustomHeadFooterView *)headerview {
    
     //  NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:headerview.tag];
    
    
    
    
      HWXiaoGroupModel *group = self.dataMuArr[headerview.tag];
      if (!(group.data.count > 0)) {
          
          [MBProgressHUD showError:@"该小号没有玩过游戏"];
          return;
          
      }
    
    
      HWViewSellNoController *SellNo = [[HWViewSellNoController alloc]init];
      SellNo.chuanmodel = group;
      [self.navigationController pushViewController:SellNo animated:YES];
    
        
    
    
}





- (void)shachuxiaohao {
    
    
       HWXiaoGroupModel *model = self.dataMuArr[self.xuanzhong];
       NSString *userID = [HWLocallyStoredData getLoginUserID];
     
        int  suiJi =  [WMUtil getRandomNumber:101 to:998];
        NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],suiJi];
        NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
        NSString *signMD5 = [WMUtil md5HexDigest:sign];
        NSDictionary *parametes = @{
                                    @"ct" :  @"app",
                                    @"ac" : @"small_del",
                                    @"ios" : @"1",
                                    @"uid" :userID,
                                    @"t" :tStr,
                                    @"sign": signMD5,
                                    @"small_uid" :model.uid
                                    
                                    };
        

        [MBProgressHUD show:@"正在删除小号"];
        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
            
          
            NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
         //   [MBProgressHUD hideHUD];
            
            
            if ([codeStr intValue] == 1) {
             
               
                [self fasongwangluoqingqiu];


            } else {
                
                 [MBProgressHUD  showError:@"加载失败"];
                
            }
            
            [self.mainaTableVie reloadData];
            NSLog(@"%@",response);
            
            
        } failure:^(NSString *error) {
          
            [MBProgressHUD  showError:@"删除失败"];
            
        }];
    
    
}






// 点击出售按钮
- (void)chushouanniu {
     
//            HWViewSellNoController *SellNo = [[HWViewSellNoController alloc]init];
//            [self.navigationController pushViewController:SellNo animated:YES];
//
    
    
}


@end
